<template>
  <a-modal
    v-model:visible="visible"
    title="新增部门"
    :maskClosable="false"
    @ok="handleOk"
    :style="{
      top: '0px',
      width: winWidth + 'px',
      height: 'auto',
    }"
  >
    <a-card
      :body-style="{ padding: '24px 32px' }"
      :bordered="false"
      :style="{
        top: '0px',
        width: '100%',
        height: winHeight + 'px',
        overflow: 'hidden',
        overflowY: 'scroll',
      }"
    >
      <a-form
        ref="formRef"
        name="basic"
        :label-col="{ span: 1 }"
        :wrapper-col="{ span: 23 }"
        autocomplete="off"
      >
        <a-form-item label="上级部门">
          <DeptSelector
            ref="deptSelector"
            id="deptSelector"
            :version="now"
            :deptId="formValue.parentId"
          />
        </a-form-item>
        <a-form-item label="部门">
          <a-input v-model:value="formValue.deptName" />
        </a-form-item>
        <a-form-item label="英文缩写">
          <a-input v-model:value="formValue.deptEn" />
        </a-form-item>
        <a-form-item label="主管">
          <a-input v-model:value="formValue.leader" />
        </a-form-item>
        <a-form-item label="电话">
          <a-input v-model:value="formValue.phone" />
        </a-form-item>
        <a-form-item label="排序">
          <a-input v-model:value="formValue.orderNum" />
          <label class="danger">数值越大展示越靠前</label>
        </a-form-item>
      </a-form>
    </a-card>
    <template #footer>
      <a-button key="submit" type="primary" :loading="loading" @click="handleOk"
        >提交</a-button
      >
      <a-button key="back" @click="handleCancel">返回</a-button>
    </template>
  </a-modal>
</template>
<script lang="ts">
import {
  defineComponent,
  ref,
  reactive,
  onMounted,
  onBeforeUnmount,
  defineEmits,
} from "vue";
import { FormValue, submit, detail } from "./index";
import ResultEnum from "@/config/ResultEnum";
import { message } from "ant-design-vue";
import { copyObjVal, timestamp } from "@/utils/common";
import DeptSelector from "@/components/deptSelector/index.vue";

export default defineComponent({
  components: { DeptSelector },
  setup(props, context) {
    const deptSelector = ref();
    const emit = defineEmits(["refreshTable"]);
    const formRef = ref();
    const visible = ref(false);
    const loading = ref(false);
    const winWidth = ref(document.body.clientWidth);
    const winHeight = ref(document.body.clientHeight - 200);

    const deptId = ref("");
    const now = ref();

    const defaultValues = () => {
      return {
        deptId: "",
        parentId: "0",
        deptName: "",
        deptEn: "",
        leader: "",
        phone: "",
        orderNum: 0,
        ancestors: "",
      };
    };

    const formValue = reactive<FormValue>(defaultValues());

    const initData = () => {
      let defaultVals = defaultValues();
      copyObjVal(defaultVals, formValue);
      detail(deptId.value)
        .then((res: any) => {
          formValue.deptId = res.deptId;
          formValue.deptName = res.deptName;
          formValue.deptEn = res.deptEn;
          formValue.leader = res.leader;
          formValue.phone = res.phone;
          formValue.orderNum = res.orderNum;
          if (res.parentId == "0") {
            formValue.parentId = "";
            formValue.ancestors = res.ancestors;
          } else {
            formValue.parentId = res.parentId;
            formValue.ancestors = res.ancestors;
          }
        })
        .catch((res: string) => {
          message.error(res);
        });
    };

    const display = (id: string) => {
      now.value = timestamp(13);
      deptId.value = id;
      visible.value = true;
      initData();
    };

    //确定按钮点击事件
    const handleOk = () => {
      let parentDept = deptSelector.value.getData();
      let postData = {
        deptId: formValue.deptId,
        parentId: parentDept == null ? 0 : parentDept.id,
        ancestors:
          parentDept == null ? "0" : parentDept.ancestors + "," + parentDept.id,
        deptName: formValue.deptName,
        deptEn: formValue.deptEn,
        leader: formValue.leader,
        phone: formValue.phone,
        orderNum: formValue.orderNum,
      };
      validateParams()
        .then((res: any) => {
          loading.value = true;
          submit(postData)
            .then((res: any) => {
              loading.value = false;
              message.success("提交成功");
              visible.value = false;
              context.emit("refreshTable");
            })
            .catch((res: string) => {
              loading.value = false;
              message.error(res);
            });
        })
        .catch((res: string) => {
          message.error(res);
        });
    };

    //校验数据
    const validateParams = () => {
      return new Promise((resolve: any, reject: any) => {
        if (!formValue.deptName) {
          reject("请填写部门");
        }
        if (!formValue.deptEn) {
          reject("请填写英文缩写");
        }
        resolve("ok");
      });
    };

    const handleCancel = () => {
      //formValue.content = "";
      console.log(defaultValues);
      visible.value = false;
    };

    onMounted(() => {
      window.onresize = () => {
        winWidth.value = document.body.clientWidth / 2;
        winHeight.value = document.body.clientHeight - 200;
      };
    });

    onBeforeUnmount(() => {});

    //返回数据
    return {
      visible,
      display,
      handleOk,
      handleCancel,
      loading,
      formValue,
      winWidth,
      winHeight,
      deptSelector,
      now,
    };
  },
});
</script>
